package com.laobiao.file_netty_lib.packet.command.download;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.laobiao.file_netty_lib.packet.command.BaseCommandPacket;
import com.laobiao.file_netty_lib.packet.command.CommandCirculationType;
import com.laobiao.file_netty_lib.packet.command.PacketCommandType;
import com.laobiao.file_netty_lib.util.BinaryConvertUtil;
import com.laobiao.file_netty_lib.util.CheckCodeUtil;

/**
 * 服务端反馈的文件下载命令对应的文件详情
 * @author 38079
 *
 */
public class ServerFileDownloadResoutCommand extends BaseCommandPacket{
	/**
	 * 文件总长度
	 */
	private long fileSize;
	/**
	 * 文件校验码
	 */
	private byte[] checkCode;
	
	public ServerFileDownloadResoutCommand() {}
	public ServerFileDownloadResoutCommand(String localFilePath,Integer businessGroup) {
		this(new File(localFilePath),businessGroup);
	}
	public ServerFileDownloadResoutCommand(File localFile,Integer businessGroup) {
		super();
		setCommand(PacketCommandType.DOWNLOAD_RESOULT);
		if(businessGroup!=null) {
			setBusinessGroup(businessGroup);
		}
		this.fileSize=localFile.length();
		this.checkCode=CheckCodeUtil.getCheckCode(localFile);
	}
	
	/**
	 * 根据参数构建服务端反馈的文件下载命令对应的文件详情
	 * @param commandGroup 参数组
	 * @param dataSize 内容长度
	 * @param checkCode 内容校验码
	 */
	public ServerFileDownloadResoutCommand(Integer commandGroup,long dataSize,byte[] checkCode) {
		super();
		setCommand(PacketCommandType.DOWNLOAD_RESOULT);
		if(commandGroup!=null) {
			setBusinessGroup(commandGroup);
		}
		this.fileSize=dataSize;
		this.checkCode=checkCode;
	}
	
	/**
	 * 根据命令包构建文件上传命令
	 * @param commandBytes
	 */
	public ServerFileDownloadResoutCommand(byte[] commandBytes) {
		ByteBuffer buf = setBaseCommand(commandBytes);
		/**
		 * 文件内容长度
		 */
		byte[] chunk=new byte[8];
		buf.get(chunk);
		setFileSize(BinaryConvertUtil.bytesToLong(chunk));//设置文件内容长度
		
		/**
		 * 校验码
		 */
		chunk=new byte[20];
		buf.get(chunk);
		setCheckCode(chunk);//设置校验码
		
	}

	public long getFileSize() {
		return fileSize;
	}

	public void setFileSize(long fileSize) {
		this.fileSize = fileSize;
	}

	public byte[] getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(byte[] checkCode) {
		this.checkCode = checkCode;
	}


	/**
	 * 长度1014
	 */
	@Override
	protected byte[] getCommContentBytes() {
		ByteBuffer buf=ByteBuffer.allocate(1014);
		buf.put(BinaryConvertUtil.longToBytes(fileSize));//文件长度
		buf.put(checkCode);//校验码
		return buf.array();
	}


	@Override
	public CommandCirculationType circulationType() {
		// TODO Auto-generated method stub
		return CommandCirculationType.PROCEDURE;
	}
	@Override
	public String toString() {
		return "ServerFileDownloadResoutCommand [fileSize=" + fileSize + ", checkCode=" + Arrays.toString(checkCode)
				+ ", circulationType()=" + circulationType() + "]";
	}
	
	
}
